//
// Created by dong on 2025/10/22.
//

#include "SManager.h"

#include "SEllipse.h"
#include "SLine.h"
#include "SRect.h"
#include "STriangle.h"

SManager::SManager()
{
    m_layout = new SLayout;
    m_name="Line";
    m_shapesBar = new ShapeBar;
    m_colorBar = new ColorBar;
}

SManager::~SManager()
{
    delete m_layout;
    delete m_shapesBar;
    delete m_colorBar;

    for (auto v: m_shapes)
    {
        delete v;
    }

    m_shapes.clear();
}

void SManager::render(SDL_Renderer* render)
{
    m_layout->render(render,SLayout::Area::Menu);
    m_layout->render(render,SLayout::Area::Draw);

    for (auto v:m_shapes)
    {
        v->render(render);
    }

    m_shapesBar->render(render);
    m_colorBar->render(render);
    SDL_Color color = m_colorBar->color();

    m_name=m_shapesBar->name();


    if (isView==true)
    {
        SDL_SetRenderDrawColor(render,color.r,color.g,color.b,0);
        SDL_Rect rect = {begin.x,begin.y,end.x-begin.x,end.y-begin.y};
        if (m_name=="Line")
        {
            SDL_RenderDrawLine(render,begin.x,begin.y,end.x,end.y);
        }else if (m_name=="Rect")
        {
            SDL_RenderDrawRect(render,&rect);

        }else if (m_name=="Ellipse")
        {
            SDL_RenderDrawEllipse(render,&rect);
        }else if (m_name=="Triangle")
        {
            SDL_RenderDrawTriangle(render,&rect);
        }else
        {

        }
    }
}

bool SManager::event(SDL_Event* msg)
{
    if (m_layout->inMenuArea(msg))
    {
        m_shapesBar->mousePressEvent(msg);
        m_colorBar->mousePressEvent(msg);
    }
    if (m_layout->inDrawArea(msg))
    {
        //绘图事件
        //鼠标左键点击+移动
        if (msg->type==SDL_MOUSEBUTTONDOWN)
        {
            if (msg->button.button==SDL_BUTTON_LEFT)
            {
                isClick = true;
                begin.x = msg->motion.x;
                begin.y = msg->motion.y;
                isView = false;
            }
        }

        if (msg->type==SDL_MOUSEBUTTONUP)
        {
            if (msg->button.button==SDL_BUTTON_LEFT)
            {
                isClick = false;
                end.x = msg->motion.x;
                end.y = msg->motion.y;
                m_shapes.push_back(createShape(begin,end,m_name));
                isView=false;
            }
        }

        if (isClick && msg->type==SDL_MOUSEMOTION)
        {
            end.x = msg->motion.x;
            end.y = msg->motion.y;
            isView = true;
        }


    }
    return  false;
}

Shape* SManager::createShape(SDL_Point begin, SDL_Point end, string name)
{
    SDL_Color color = m_colorBar->color();
    SDL_Rect rect = {begin.x,begin.y,end.x-begin.x,end.y-begin.y};
    if (name=="Line")
    {
        return  new SLine(begin,end,color);
    }else if (name=="Rect")
    {
        return new SRect(rect,color);
    }else if (name =="Ellipse")
    {
        return new SEllipse(rect,color);
    }else if (name=="Triangle")
    {
        return new STriangle(rect,color);
    }

    return nullptr;
}
